#ifndef HASHROWIMPL_H_JM_20120910
#define HASHROWIMPL_H_JM_20120910

#include "HashRow.h"
#include "HashRowSynchronizedImpl.h"

namespace jm4bbx {

	/**
	* 
	* @tparam T typ ukladanych hodnot.
	**/
	template<class T>
	class HashRowImpl : public HashRow<T> {
	public:

		HashRowImpl() : synchronizedThis_(this) { }

		virtual int add(const ValueType& value) override {
			for (size_t i = 0; i < values_.size(); ++i) {
				if (values_[i].first == value) {					
					return ++values_[i].second;;
				}
			}
			values_.push_back(ExtendedValueType(value, 1));
			return 1;
		}

		virtual ExtendedValueType getExtendedValue(const size_t& index) const override {
			return values_[index];
		}

		virtual bool isEmpty() const override {
			return values_.empty();
		}

		virtual void clear() override {
			values_.clear();
		}

		virtual size_t distinctSize() const override {
			return values_.size();
		}

		std::pair<int, int> foreach(ForechValueFunType functor, const std::pair<int, int>& iterationContext) override {
			for (size_t i = iterationContext.first; i < values_.size(); ++i) {
				for (size_t j = iterationContext.second; j < values_[i].second; ++j) {
					if (!functor(values_[i].first)) {
						return std::make_pair(i,j);
					}
				}				
			}
		}

		int foreachExtended(ForechExtendedValueFunType functor, int iterationContext) override {
			for (size_t i = iterationContext; i < values_.size(); ++i) {
				if (!functor(values_[i])) {
					return i;
				}			
			}
		}

		virtual HashRow<T>* getLockFreeInstance() override { 
			return this; 
		};

		virtual HashRow<T>* getLockableInstance() override { 
			return &synchronizedThis_; 
		};

	private:
		/// Hodnoty v riadku
		MultiValueType values_;
		HashRowSynchronizedImpl<T> synchronizedThis_;
	};



} // namespace jm4bbx


#endif
